<?
$pageName = "courseClass";
include ROOT . '/view/admin/common/header.php';
?>
<?
/**
 * @var	CourseClass	$data
 * @var Teacher $teacher
 * @var Classroom   $classroom
 * @var $classTimes
 * @var ClassTime   $classTime
 */
?>

<div class="container">
    <br /><br />
    <? if(isset($_GET['succ'])) { ?><div class="alert alert-success" role="alert">操作成功！</div><? }?>
    <div class="page-header">
        <a href="/admin/courseClass/" class="btn btn-warning" role="button" style="float:right;">返回开课班列表</a>
        <h2><?= ($data->id) ? "修改开课班" : "创建开课班";?></h2>
    </div>
    <div class="row" style="margin-bottom:20px;">
        <div class="col-xs-8">
            <form class="form-horizontal" role="form" method="post">
                <? if ($data->id) { ?>
                <div class="form-group">
                    <label class="col-sm-2 control-label">ID</label>
                    <div class="col-sm-10">
                        <input name="id" type="text" class="form-control input-lg" value="<?=$data->id?>" readonly="readonly">
                    </div>
                </div>
                <? } ?>
                <div class="form-group">
                    <label class="col-sm-2 control-label">课程</label>
                    <div class="col-sm-10">
                        <select name="courseId" class="form-control input-lg">
                            <? foreach (Course::find(new TrueQuery()) as $course) { ?>
                                <option value="<?=$course->id?>" <?=($data->courseId == $course->id) ? 'selected' : ''?> ><?=$course->name?></option>
                            <? } ?>
                        </select>
                    </div>
                </div>
                <?
                $show = [];
                $show['学期'] = 'semester';
                $show['最大容纳人数'] = 'capacity';
                foreach ($show as $key => $value) {?>
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><?=$key?></label>
                        <div class="col-sm-10">
                            <input name="<?=$value?>" type="text" class="form-control input-lg" value="<?=$data->$value?>"/>
                        </div>
                    </div>
                <? } ?>
                <div class="form-group">
                    <label class="col-sm-2 control-label">任课教师</label>
                    <div class="col-sm-10">
                        <select name="teacherId" class="form-control input-lg">
                            <? foreach (Teacher::find(new TrueQuery()) as $teacher) { ?>
                                <option value="<?=$teacher->id?>" <?=($data->teacherId == $teacher->id) ? 'selected' : ''?> ><?=$teacher->name?></option>
                            <? } ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">教室</label>
                    <div class="col-sm-10">
                        <select name="classroomId" class="form-control input-lg">
                            <? foreach (Classroom::find(new TrueQuery()) as $classroom) { ?>
                                <option value="<?=$classroom->id?>" <?=($data->classroomId == $classroom->id) ? 'selected' : ''?> ><?=$classroom->name?></option>
                            <? } ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">上课时间</label>
                    <div class="col-sm-9">
                        <table class="table table-striped" id="classTimes">
                            <thead>
                            <th>星期</th>
                            <th>开始时间</th>
                            <th>结束时间</th>
                            <th></th>
                            </thead>
                        </table>
                    </div>
                    <button type="button" class="col-sm-1 btn btn-info" onclick="addTime();">添加</button>
                </div>
                <p>&nbsp;</p>
                <div class="form-group">
                    <div class="col-sm-offset-1 col-sm-11">
                        <button type="submit" class="btn btn-danger btn-lg btn-block">提交！</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <!-- .row -->
    <script type="text/javascript">
        function addTime() {
            var table = document.getElementById('classTimes');
            var row = table.insertRow();
            var cell = row.insertCell(0);
            cell.innerHTML = "<select name=\"day[]\" class=\"input-lg\">" +
<?
                    for ($i = 1; $i <= 7; $i++) {
                        echo "                \"<option value='{$i}'>星期{$i}</option>\" + \n";
                    }
                ?>"\n</select>";
            cell = row.insertCell(1);
            cell.innerHTML = "<select name=\"startTimes[]\" class=\"input-lg\">" +
<?
				for ($i = 1; $i <= 11; $i++) {
					echo "                \"<option value='{$i}'>第{$i}节</option>\" + \n";
				}
?>
                "\n</select>";
            cell = row.insertCell(2);
            cell.innerHTML = "<select name=\"endTimes[]\" class=\"input-lg\">" +
<?
								for ($i = 1; $i <= 11; $i++) {
									echo "                \"<option value='{$i}'>第{$i}节</option>\" + \n";
								}
?>
                "\n</select>";
            cell = row.insertCell(3);
            cell.innerHTML = "<button class='btn btn-danger btn-lg' type='button' onclick='deleteRow(this.parentElement.parentElement.rowIndex);'>删除</button>";
        }
        function deleteRow(rowIndex) {
            var table = document.getElementById('classTimes');
            table.deleteRow(rowIndex+1);
        }
        function loadClassTimes() {
            var table = document.getElementById('classTimes');
            var row = table.rows[1];
<?
            if ($classTimes) {
                $classTime = $classTimes[0];
                /**
                 *
                 * @var ClassTime   $classTime
                 */
                echo "                row.cells[0].childNodes[0].selectedIndex = " . ($classTime->day-1) . ";\n";
                echo "                row.cells[1].childNodes[0].selectedIndex = " . ($classTime->startTime-1) . ";\n";
                echo "                row.cells[2].childNodes[0].selectedIndex = " . ($classTime->endTime-1) . ";\n";
                if (count($classTimes) > 1) {
                    unset($classTimes[0]);
                    $rowIndex = 2;
                    foreach ($classTimes as $classTime) {
                        echo "                addTime();\n";
                        echo "                row = table.rows[{$rowIndex}];\n";
                        echo "                row.cells[0].childNodes[0].selectedIndex = " . ($classTime->day-1) . ";\n";
                        echo "                row.cells[1].childNodes[0].selectedIndex = " . ($classTime->startTime-1) . ";\n";
                        echo "                row.cells[2].childNodes[0].selectedIndex = " . ($classTime->endTime-1) . ";\n";
                    }
                }
            }
?>
        }
        addTime();
        loadClassTimes();
    </script>
</div>
<? include ROOT . '/view/admin/common/footer.php'; ?>
